package com.jit.app.frament;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jit.app.AppDetailActivity;
import com.jit.app.R;
import com.jit.app.model.AppInfoModel;
import com.jit.app.model.Constants;
import com.jit.app.util.DateFormatUtils;
import com.jit.app.util.JSONResult;

import java.util.Map;

public class DetailFragment extends Fragment {

    AppInfoModel appInfoModel;
    TextView categoryLevel1TextView;
    TextView categoryLevel2TextView;
    TextView categoryLevel3TextView;
    TextView versionNoTextView;
    TextView updateDateTextView;
    TextView appInfoTextView;
    TextView devNameTextView;
    Handler handler;
    AppDetailActivity activity;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = (AppDetailActivity) getActivity();
        View view = inflater.inflate(R.layout.fragment_detail,null);

        initView(view);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                switch (msg.what){
                    case Constants.MessageWhat.APPINFO_LOAD_SUCCESS:
                        JSONResult jsonResult = (JSONResult) msg.obj;
                        if(jsonResult.getCode() == Constants.ResultCode.SUCCESS){
                            Map appInfo = (Map) jsonResult.getData().get("appInfoDetail");
                            categoryLevel1TextView.setText((String) appInfo.get("categoryLevel1Name"));
                            categoryLevel2TextView.setText((String) appInfo.get("categoryLevel2Name"));
                            categoryLevel3TextView.setText((String) appInfo.get("categoryLevel3Name"));
                            versionNoTextView.setText((String) appInfo.get("versionNo"));
                            updateDateTextView.setText(DateFormatUtils.toHours((Long) appInfo.get("updateDate"))+"小时之前");
                            appInfoTextView.setText((String) appInfo.get("appInfo"));
                            devNameTextView.setText((String) appInfo.get("devName"));
                        }
                        break;
                }
                return true;
            }
        });

        initData();

        return view;
    }

    public void initView(View view){
        categoryLevel1TextView = view.findViewById(R.id.tv_categorylevel1);
        categoryLevel2TextView = view.findViewById(R.id.tv_categorylevel2);
        categoryLevel3TextView = view.findViewById(R.id.tv_categorylevel3);
        versionNoTextView = view.findViewById(R.id.tv_versionNo);
        updateDateTextView = view.findViewById(R.id.tv_updatedate);
        appInfoTextView = view.findViewById(R.id.tv_appinfo);
        devNameTextView = view.findViewById(R.id.tv_devName);
    }

    public void initData(){
        appInfoModel = new AppInfoModel();
        appInfoModel.loadAppInfoDetail(handler,(Integer) activity.appInfo.get("id"));
    }

}
